﻿using System;
using System.Data.SQLite;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace PwManager
{
    public partial class Register : Form
    {
        string Account = string.Empty;
        public Register(string account)
        {
            InitializeComponent();

            Account = account;
            if (!string.IsNullOrWhiteSpace(account))
            {
                txtEmail.Text = account;
                txtEmail.ReadOnly = true;

                Text = "Retrieve";
                btnRegister.Text = "确认";
            }
        }

        string verificateCode = string.Empty;

        private void llbSendEmail_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var regex = @"^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$";
            var email = txtEmail.Text.Trim();
            if (!Regex.IsMatch(email, regex))
            {
                MessageBox.Show("邮箱格式错误");
                return;
            }
            try
            {
                var body = VerificationCodeHelper.Generate(6);
                verificateCode = body;
                EmailHelper.SendEmailMsg(email, "StrongBox Register", $"StrongBox验证码：{body}");

                var index = txtEmail.Text.IndexOf('@');

                MessageBox.Show($"邮件已发送到你的***{txtEmail.Text.Substring(index - 3)}");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"邮件发送失败：{ex.ToString()}");
            }
        }

        private void btnRevert_Click(object sender, EventArgs e)
        {
            txtVerifiCode.Text = "";
            txtPwd.Text = "";
            txtConfirm.Text = "";
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            #region {验证}
            if (string.IsNullOrWhiteSpace(txtVerifiCode.Text))
            {
                MessageBox.Show("请输入验证码");
                return;
            }
            if (txtVerifiCode.Text.ToUpper() != verificateCode.ToUpper())
            {
                MessageBox.Show("验证码错误，请重新获取");
                verificateCode = "";
                return;
            }

            if (string.IsNullOrWhiteSpace(txtPwd.Text))
            {
                MessageBox.Show("请输入密码");
                return;
            }

            if (txtPwd.Text != txtConfirm.Text)
            {
                MessageBox.Show("两次密码不一样");
                return;
            }
            #endregion

            if (String.IsNullOrWhiteSpace(Account))
                FirstRegister();
            else
                Retrieve();

            Close();
        }

        private void FirstRegister()
        {
            //初始化账号密码
            string sql = @"insert into strongbox(title,account,pwd,remark) values (@title,@account,@pwd,@remark);";
            var pwd = EncryptHelper.MD5_Encrypt(txtPwd.Text);
            var paras = new SQLiteParameter[]
            {
                new SQLiteParameter("@title","登录账号"),
                new SQLiteParameter("@account",txtEmail.Text),
                new SQLiteParameter("@pwd",pwd),
                new SQLiteParameter("@remark","strongbox系统登录账号，将不显示到系统中。。。。"),
            };
            SQLiteHelper.ExecuteNonQuery(sql, paras);

            MessageBox.Show("注册成功");
        }

        private void Retrieve()
        {
            //初始化账号密码
            string sql = @"update strongbox set pwd=@pwd where id=1";
            var pwd = EncryptHelper.MD5_Encrypt(txtPwd.Text);
            var paras = new SQLiteParameter[]
            {
                new SQLiteParameter("@pwd",pwd)
            };
            SQLiteHelper.ExecuteNonQuery(sql, paras);

            MessageBox.Show("修改成功");
        }
    }
}
